package com.heima.common.util;

import io.jsonwebtoken.*;

public class Test {
    public static void main(String[] args) {
        // 生成token
        String json = Jwts.builder()
                .setId("id10010")
                        .setIssuer("app")
                        .setSubject("hello")
                        .setAudience("user")
                        .claim("userId",10010)
                        .signWith(SignatureAlgorithm.HS256,"mima")
                .compact();
        System.out.println(json);

        json = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJpZDEwMDEwIiwiaXNzIjoiYXBwIiwic3ViIjoiaGVsbG8iLCJhdWQiOiJ1c2VyIiwidXNlcklkIjoxMDAxMH0.xFHH98VGaovPT8EXDzrMraY";

        // 解析token
        // Jwt jwt = Jwts.parser()
        //         .setSigningKey("mima")
        //         .parse(json);
        Jws<Claims> claimsJws = Jwts.parser()
                .setSigningKey("mima")
                .parseClaimsJws(json);
        Claims body = claimsJws.getBody();
        Object userId = body.get("userId");
        System.out.println(userId);
    }
}
